package br.com.bolsamovel;

import java.text.NumberFormat;

import org.droidsql.DBHelpper;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;

public class DepositWithdrawHistoryActivity extends Activity {
	SimpleCursorAdapter adp;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.depositwithdraw_history);
		
		ListView listView = (ListView) findViewById(R.id.depwith_hist_listView);
		
		adp = new SimpleCursorAdapter(this,R.layout.lst_depositwithdraw_hist_layout,null,new String[]{"signal","transf_date","transf_value","funds_from"} , new int[]{R.id.dpwd_signal,R.id.dpwd_date,R.id.dpwd_value,R.id.dpwd_tofrom});

		adp.setViewBinder(new ViewBinder() {
			Drawable redArrow = getResources().getDrawable(R.drawable.red_arrows_right);
			Drawable greenArrow = getResources().getDrawable(R.drawable.grenn_arrows_left);
			

			NumberFormat decFormat = NumberFormat.getCurrencyInstance();
			
			public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
				String colName = cursor.getColumnName(columnIndex);
				
				
				if( "signal".equals(colName)){
					if( cursor.getInt(0) > 0){
						((ImageView)view).setImageDrawable(greenArrow);
					}else{
						((ImageView)view).setImageDrawable(redArrow);
					}
					
					return true;
				}else if( "transf_date".equals(colName)){
					long value = cursor.getLong(columnIndex);
					
					TextView tv = (TextView) view;
					
					if(value > 0){
						tv.setText( DateUtils.formatAsDate(value));
					}else{
						tv.setText("");
					}
				}else if("transf_value".equals(colName)){
					double value = cursor.getDouble(columnIndex);
					TextView tv = (TextView) view;
					
					tv.setText(decFormat.format(value));
				}else{
					TextView tv = (TextView) view;
					tv.setText(cursor.getString(columnIndex));
				}
				
				return true;
			}
		});
		
		listView.setAdapter(adp);
		
		listView.setOnItemClickListener(new OnItemClickListener(){
			public void onItemClick(AdapterView<?> av, View v, int pos,	long id) {
			}});
		
	}

	@Override
	protected void onRestart() {
		super.onRestart();
		onShow();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		onShow();
	}
	
	private void onShow(){
		Cursor c = DBHelpper.getInstance().getConnection().rawQuery("select signal,_id,transf_date,transf_value,funds_from from funds_transfer where account_id = "+getIntent().getLongExtra("account_id", 0)+" order by transf_date desc", null);
		
		adp.changeCursor(c);
	}
}